<?php

session_start();
require_once '../MyDB.php';
$con = new MyDB();

$type = $_POST["type"];

if ($type == 1) {
    // Inserta
    $checkin = $_POST["checkin"];
    $checkout = $_POST["checkout"];
    $idcliente = $_POST["idcliente"];
    $idusuario = $_POST["idusuario"];
    $idorigenreserva = $_POST["idorigenreserva"];
    $idroom = $_POST["idroom"];
    $cantidad = $_POST["cantidad"];
    $status = $_POST["status"];

    $query = $con->insertInto("reservas", $fields = array(
        "Checkin" => $checkin,
        "Checkout" => $checkout,
        "IdCliente" => $idcliente,
        "IdUsuario" => $idusuario,
        "IdOrigenReserva" => $idorigenreserva,
        "IdRoom" => $idroom,
        "Cantidad" => $cantidad,
        "Status" => $status));

//    print_r($query["sql"]);

    $result = $query["status"];
    if ($result == "success") {
        echo $query["id"];
    } else {
        echo "El dato no ha sido guardado";
    }
} elseif ($type == 0) {
    // Búsqueda todas las reservas por año y mes

    $ano = $_POST["ano"];
    $dia = (strlen($_POST["dia"]) == 1) ? "0" . $_POST["dia"] : $_POST["dia"];
    $mes = (strlen($_POST["mes"]) == 1) ? "0" . $_POST["mes"] : $_POST["mes"];


    $SQL = "SELECT * FROM  `reservas` "
            . "INNER JOIN clientes ON clientes.IdCliente = reservas.IdCliente "
            . "WHERE ((SUBSTRING( Checkin, -10, 4 ) =  '" . $ano . "' "
            . "AND SUBSTRING( Checkin, -5, 2 ) =  '" . $mes . "') "
            . "OR  (SUBSTRING( Checkin, -10, 4 ) =  '" . $ano . "' "
            . "AND SUBSTRING( Checkin, -5, 2 ) !=  SUBSTRING( Checkout, -5, 2 ) "
            . "AND SUBSTRING( Checkout, -5, 2 ) =  '" . $mes . "')) "
//            . "AND (str_to_date(Checkout, '%Y-%m-%d')>str_to_date(now(), '%Y-%m-%d')) "
            . "AND status = 1 "
            . "ORDER BY reservas.IdReserva";
    $query = $con->customSelectFrom($SQL);

    $result = $query["result"];
    //print_r($query);
    $lista = array();
    if ($query["num"] > 0) {
        foreach ($result as $row) {


            $param = "" . $row["IdReserva"] . " " . $row["Checkin"] . " " . $row["Checkout"] . " " . $row["IdCliente"] . " "
                    . $row["IdOrigenReserva"] . " " . $row["IdRoom"] . " " . $row["Cantidad"] . " ". $row["Nombre"] . " ". $row["Apellido"] . "";

            array_push($lista, $param);
        }
        echo json_encode($lista);
    }
} elseif ($type == 2) {
    // Borra
    $id = $_POST["id"];
    //$query = $con->deleteFrom("clientes", $where = array("IdCliente" => $id), $like = false, $limit = 1);
    $query = $con->updateTable("reservas", $fields = array("Status" => 0), $where = array("IdReserva" => $id), $like = false, $limit = 1);
    $result = $query["status"];

    //print_r($query['sql']);
    if ($result == "success") {
        echo 'Dato borrado con éxito';
    } else {
        echo "El dato no ha sido borrado";
    }
} elseif ($type == 3) {
    // Modificar
    $id = $_POST["id"];
    $checkin = $_POST["checkin"];
    $checkout = $_POST["checkout"];
    $idcliente = $_POST["idcliente"];
    $idusuario = $_POST["idusuario"];
    $idorigenreserva = $_POST["idorigenreserva"];
    $idroom = $_POST["idroom"];
    $cantidad = $_POST["cantidad"];
    $status = $_POST["status"];

    $query = $con->updateTable("clientes", $fields = array(
        "Checkin" => $checkin,
        "Checkout" => $checkout,
        "IdCliente" => $idcliente,
        "IdUsuario" => $idusuario,
        "IdOrigenReserva" => $idorigenreserva,
        "IdRoom" => $idroom,
        "Cantidad" => $cantidad,
        "Status" => $status), $where = array("IdReserva" => $id));
    $result = $query["status"];
    if ($result == "success") {
        echo 'Dato guardado con éxito';
    } else {
        echo "El dato no ha sido guardado";
    }
} elseif ($type == 4) {
    // busqueda por id => 4 retorna fila

    $id = $_POST["id"];

    $SQL = "SELECT * FROM  reservas r INNER JOIN clientes c ON "
            . "r.IdCliente = c.IdCliente WHERE IdReserva = " . $id;
    $query = $con->customSelectFrom($SQL);

    $result = $query["result"];
    if ($query["num"] > 0) {
        foreach ($result as $row) {
            echo '<table>';
            echo '<tr><td> <b>Cliente:</b> ' . $row["Nombre"] . " " . $row["Apellido"] . '</td></tr>';
            echo '<tr><td> <b>Checkin:</b> ' . $row["Checkin"] . '</td></tr>';
            echo '<tr><td> <b>Checkout:</b> ' . $row["Checkout"] . '</td></tr>';
            echo '<tr><td> <b>Cantidad:</b> ' . $row["Cantidad"] . '</td></tr>';
            echo '<tr><td> <b>Mail:</b> ' . $row["Mail"] . '</td></tr>';
            echo '<tr><td> <b>Teléfono:</b> ' . $row["Telefono"] . '</td></tr>';
            echo '</table>';
        }
    }
} elseif ($type == 5) {
    // busqueda por id => 4 retorna fila
    // busqueda por id => 5 retorna JSON

    $id = $_POST["id"];

    $SQL = "SELECT *, (Select count(IdRegistracion) from registracionesclientes r Where r.IdReserva = ".$id.") CantCamas "
            . "FROM  reservas r INNER JOIN clientes c ON "
            . "r.IdCliente = c.IdCliente WHERE IdReserva = " . $id;
    $query = $con->customSelectFrom($SQL);

    $result = $query["result"];
    if ($query["num"] > 0) {
        foreach ($result as $row) {

            $param = "" . $row["IdReserva"] . " " . $row["Checkin"] . " " . $row["Checkout"] . " " . $row["IdCliente"] . " "
                    . $row["Nombre"] . " " . $row["Apellido"] . " " . $row["Cantidad"] . " " . $row["CantCamas"]. " " . $row["IdRoom"];
            echo json_encode($param);
        }
    }
}
?>